package com.ls.fw.data.statement;

import java.io.Closeable;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import com.ls.fw.data.bean.Record;
import com.ls.fw.data.bean.Table;
import com.ls.fw.data.visitor.RecordVisitor;
import com.ls.fw.data.visitor.StatementVisitor;


/**
 * 更新实体
 * @author ls
 * 2014年11月21日下午3:15:31
 */
public class Update extends BaseWhere implements Closeable,Statement{

	private List<Record> records = new ArrayList<Record>();

	private Record record = null;
	
	/**
	 * 添加一条新记录
	 * @author ls
	 * 2014年11月24日下午5:16:37 
	 * @param Record
	 * @return
	 */
	public boolean addRecord(Record record){
		return this.records.add(record);
	}
	
	/**
	 * 开始添加一条新记录
	 * @author ls
	 * 2014年11月24日下午5:16:04 
	 * @return
	 */
	public Record startRecord(){
		record = new Record();
		this.records.add(record);
		return record;
	}
	
	public List<Record> getRecords() {
		return records;
	}

	public void setRecords(List<Record> records) {
		this.records = records;
	}

	@Override
	public String toString() {
		return "Update [Records=" + records + "]"+super.toString();
	}

	@Override
	public void close() throws IOException {
		records.clear();
	}
	
	@Override
	public void accept(StatementVisitor statementVisitor) {
		statementVisitor.visit(this);
	}
	
	
	/**
	 * 遍历记录
	 * @author ls
	 * 2014年12月1日上午11:28:25 
	 * @param recordVisitor
	 */
	public int accept(RecordVisitor recordVisitor) {
		if(this.records != null && !records.isEmpty()){
			for (Record field : records) {
				recordVisitor.visit(field);
			}
			return records.size();
		}
		return 0;
	}

	/**
	 * @param table the table to set
	 */
	public void setFromItem(Table table) {
		from.getTables().clear();
		from.getFromItems().clear();
		from.addFromItem(table);
	}
	
	public void setTable(String schemaName, String name) {
		this.setFromItem(schemaName, name);
	}
	
	public void setTable(String name) {
		this.setFromItem(name);
	}
	
	public void setFromItem(String schemaName, String name) {
		from.getTables().clear();
		from.getFromItems().clear();
		from.addFromItem(schemaName, name);
	}
	
	public void setFromItem(String name) {
		from.getTables().clear();
		from.getFromItems().clear();
		from.addFromItem(name);
	}
	
	public Table getTable(){
		return this.from.getTable();
	}
	
	public boolean hasRecord(){
		return (records!=null && !records.isEmpty())?true:false;
	}
}
